import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * This class contains the main operations of the devices.
 * 
 * @author Christian Pérez Zavalija
 * @version 25-Jun-2013
 */
public class Device extends Actor
{
    protected Pin[] pins;
    protected Switch[] switches;
    private int cantpins;
    private int distance;
    private int distanceY;
    public int x;
    public int y;
    
    /**
     * Pin arrangement adds to the world with their respective distance between them and the width of the
     * device. if is a DipSwitch add its swhitches.
     */
    public void addedToWorld(World world)
    {
        if(cantpins==16){
             y=getY()+27;
             x=getX()-42;}
        if(cantpins==10){
             y=getY()+40;
             x=getX()-24;}
        if(cantpins==2){
             y=getY()+21;
             x=getX();}
        for(int i=0;i<cantpins;i++){
            if(cantpins==16)
                if(i==(cantpins/2)){
                    y-=distanceY;
                    x=x-(distance*i);
                }
            if(cantpins==10)
                if(i==(cantpins/2)){
                    y-=distanceY;
                    x=x-(distance*i);
                }
            if(cantpins==2)
                if(i==(cantpins/2)){
                    y-=distanceY;
                    x=x-(distance*i);
                }
            if(cantpins==16)
                if(i<(cantpins/2))
                    getWorld().addObject(switches[i],x+1,getY());
            getWorld().addObject(pins[i],x,y);
            x+=distance;
        }
    }
    
    /**
     * Creates pins and switch over to a pin count and saves arrglos and instance variables are
     * initialized with the values ​​of the parameters.
     * 
     * @ Param enter a quantity of pins and the distances in X and Y.
     */
    public Device(int aCantpins,int aDistanceX,int aDistanceY)
    {
        pins=new Pin[aCantpins];        //Arreglo del tamaño de pins qe tiene
        for(int i=0;i<aCantpins;i++){
            Pin pin=new Pin(9);         //Inicializar nuevo objeto pin con el tamaño de pin
            pins[i]=pin;
        }
        if(aCantpins==16){
            switches=new Switch[aCantpins/2];
            for(int i=0;i<8;i++){
                Switch dswitch=new Switch();         //Inicializar nuevo objeto switch para el DipSwitch
                switches[i]=dswitch;
            }
        }
        cantpins=aCantpins;
        distance=aDistanceX;
        distanceY=aDistanceY;
    }
    
    /**
     * This method calls the method scale() of its subclass.
     */
    public void scale()
    {  }
    
    /**
     * Accommodates the pins relative to the size and number of pins has.
     */
    public void acomodaPins()
    {
        /*int y=getY()+27;
        int x=getX()-42;*/
        if(cantpins==16){
             y=getY()+27;
             x=getX()-42;}
        if(cantpins==10){
             y=getY()+40;
             x=getX()-24;}
        if(cantpins==2){
             y=getY()+21;
             x=getX();}
        for(int i=0;i<cantpins;i++){
            if(i==(cantpins/2)){
                y-=distanceY;
                x=x-(distance*i);
            }
            if(cantpins==16)
                if(i<(cantpins/2))
                    switches[i].setLocation(x+1,getY());
            pins[i].setLocation(x,y);
            x+=distance;
        }
    }
    
    /**
     * Checks the number of clicks. If 1 click the switch is set at ON state and if you are 2 clicks is
     * placed in shutdown state.
     * 
     * @param into a button, that indicates which button was pressed.
     */
    public void checkClickDS(int aButton)
    {
        MouseInfo mouse=Greenfoot.getMouseInfo();
        if(mouse.getClickCount()==1){
                switches[aButton].ON();
                pins[aButton].charge=1;
        }
        else if(mouse.getClickCount()==2){
                switches[aButton].OFF();
                pins[aButton].charge=0;
            }
    }
    
    /**
     * Permit drag each device with their respective pins.
     */
    public void act() 
    {
        if(Greenfoot.mouseDragged(this))    //Condition so that the object can be dragged with the mouse while pressing the left button.
        {
            MouseInfo mouse=Greenfoot.getMouseInfo();
            setLocation(mouse.getX(),mouse.getY());
            acomodaPins();
        }
    }    
}
